package com.lanmaoly.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 启动类
 *
 * @author Administrator
 * //@MapperScan(basePackages = {"com.lanmaoly.web.*.mapper"})
 */
@SpringBootApplication
public class ArbitrationBossApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(ArbitrationBossApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ArbitrationBossApplication.class);
    }

    @EnableAsync
    @Configuration
    public class TaskExecutePoolConfig {

        @Bean
        public Executor taskAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            // 核心线程池大小
            executor.setCorePoolSize(4);
            // 最大线程数
            executor.setMaxPoolSize(8);
            // 队列容量
            executor.setQueueCapacity(1000);
            // 活跃时间
            executor.setKeepAliveSeconds(30);
            // 线程名字前缀
            executor.setThreadNamePrefix("portal-data-executor-");
            // 优雅关停
            executor.setWaitForTasksToCompleteOnShutdown(true);
            executor.setAwaitTerminationSeconds(60);
            // setRejectedExecutionHandler：当pool已经达到max size的时候，如何处理新任务
            // CallerRunsPolicy：不在新线程中执行任务，而是由调用者所在的线程来执行
            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            executor.initialize();
            return executor;
        }

    }
}